package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.ShopArticleDao;
import guda.shop.cms.entity.ShopArticle;
import guda.shop.cms.entity.ShopArticleContent;
import guda.shop.cms.entity.ShopChannel;
import guda.shop.cms.manager.ShopArticleContentMng;
import guda.shop.cms.manager.ShopArticleMng;
import guda.shop.cms.manager.ShopChannelMng;
import guda.shop.common.hibernate3.Updater;
import guda.shop.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ShopArticleMngImpl
        implements ShopArticleMng {
    private ShopArticleContentMng shopArticleContentMng;
    private ShopChannelMng shopChannelMng;
    private ShopArticleDao shopArticleDao;

    @Transactional(readOnly = true)
    public Pagination getPage(Long paramLong1, Long paramLong2, int paramInt1, int paramInt2) {
        Pagination localPagination = this.shopArticleDao.getPage(paramLong1, paramLong2, paramInt1, paramInt2);
        return localPagination;
    }

    @Transactional(readOnly = true)
    public Pagination getPageForTag(Long paramLong1, Long paramLong2, int paramInt1, int paramInt2) {
        Pagination localPagination;
        if (paramLong2 != null)
            localPagination = this.shopArticleDao.getPageByChannel(paramLong2, paramInt1, paramInt2, true);
        else
            localPagination = this.shopArticleDao.getPageByWebsite(paramLong1, paramInt1, paramInt2, true);
        return localPagination;
    }

    public List<ShopArticle> getListForTag(Long paramLong1, Long paramLong2, int paramInt1, int paramInt2) {
        List localList;
        if (paramLong2 != null)
            localList = this.shopArticleDao.getListByChannel(paramLong2, paramInt1, paramInt2, true);
        else
            localList = this.shopArticleDao.getListByWebsite(paramLong1, paramInt1, paramInt2, true);
        return localList;
    }

    @Transactional(readOnly = true)
    public ShopArticle findById(Long paramLong) {
        ShopArticle localShopArticle = this.shopArticleDao.findById(paramLong);
        return localShopArticle;
    }

    public ShopArticle save(ShopArticle paramShopArticle, Long paramLong, String paramString) {
        ShopChannel localShopChannel = this.shopChannelMng.findById(paramLong);
        paramShopArticle.setChannel(localShopChannel);
        paramShopArticle.init();
        this.shopArticleDao.save(paramShopArticle);
        if (paramString != null)
            this.shopArticleContentMng.save(paramString, paramShopArticle);
        return paramShopArticle;
    }

    public ShopArticle update(ShopArticle paramShopArticle, Long paramLong, String paramString) {
        ShopArticle localShopArticle = findById(paramShopArticle.getId());
        ShopArticleContent localShopArticleContent = localShopArticle.getArticleContent();
        if (localShopArticleContent != null)
            localShopArticleContent.setContent(paramString);
        else
            this.shopArticleContentMng.save(paramString, localShopArticle);
        if (paramLong != null)
            localShopArticle.setChannel(this.shopChannelMng.findById(paramLong));
        Updater localUpdater = new Updater(paramShopArticle);
        localShopArticle = this.shopArticleDao.updateByUpdater(localUpdater);
        return localShopArticle;
    }

    public ShopArticle deleteById(Long paramLong) {
        ShopArticle localShopArticle = this.shopArticleDao.deleteById(paramLong);
        return localShopArticle;
    }

    public ShopArticle[] deleteByIds(Long[] paramArrayOfLong) {
        ShopArticle[] arrayOfShopArticle = new ShopArticle[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfShopArticle[i] = deleteById(paramArrayOfLong[i]);
            i++;
        }
        return arrayOfShopArticle;
    }

    @Autowired
    public void setDao(ShopArticleDao paramShopArticleDao) {
        this.shopArticleDao = paramShopArticleDao;
    }

    @Autowired
    public void setShopChannelMng(ShopChannelMng paramShopChannelMng) {
        this.shopChannelMng = paramShopChannelMng;
    }

    @Autowired
    public void setShopArticleContentMng(ShopArticleContentMng paramShopArticleContentMng) {
        this.shopArticleContentMng = paramShopArticleContentMng;
    }
}

